/**
 *
 */
package com.hellojava.util.web;

import com.hellojava.bean.ModuleProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 请求拦截器，设置当前请求类型、来源、权限等信息
 */
@Component
public class RequestContext extends HandlerInterceptorAdapter {
    private static final ThreadLocal<ModuleProperties> threadModuleProperties = new ThreadLocal<>();

    private static final ThreadLocal<RequestInfo> threadRequestInfo = new ThreadLocal<>();

    private static final ThreadLocal<HttpMethodInfo> threadHttpMethodInfo = new ThreadLocal<>();

    @Autowired
    private ModuleProperties moduleProperties;

    public static RequestInfo getRequestInfo() {
        return threadRequestInfo.get();
    }

    public static HttpMethodInfo getHttpMethodInfo() {
        return threadHttpMethodInfo.get();
    }

    public static ModuleProperties getModuleProperties() {
        return threadModuleProperties.get();
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        resourcesContext(request);

        threadRequestInfo.set(RequestInfo.parse(request));
        threadHttpMethodInfo.set(HttpMethodInfo.parse((HandlerMethod) handler));

        if (threadModuleProperties.get() == null) {
            threadModuleProperties.set(moduleProperties);
        }

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {// 设置session超时
        threadRequestInfo.remove();
        threadHttpMethodInfo.remove();
    }

    private void resourcesContext(HttpServletRequest request) {
        request.setAttribute("domain", moduleProperties.domain);
        request.setAttribute("static", moduleProperties.staticDomain);
    }
}
